package com.syezon.wifikey.wifi.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.wifikey.R;
import com.syezon.wifikey.home.info.WifiListInfo;
import com.syezon.wifikey.imageloader.ImageLoader;
import com.syezon.wifikey.wifi.info.WifiInfo;
import com.syezon.wifikey.wifi.WifiSupport;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/9/7.
 */

public class WifiListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<WifiListInfo> mData = new ArrayList<>();
    private ItemClickListener mItemClickListener;
    private AdInfo mAdInfo;

    public WifiListAdapter(Context context) {
        this.mContext = context;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        if (itemClickListener != null) {
            mItemClickListener = itemClickListener;
        }
    }

    public void setData(List<WifiListInfo> wifiBeans) {
        if (wifiBeans != null && wifiBeans.size() > 0) {
            mData.clear();
            mData.addAll(wifiBeans);
            notifyDataSetChanged();
        }
        if (mAdInfo != null && mData.size() > 3) {
            WifiListInfo wifiListInfo = new WifiListInfo();
            wifiListInfo.setType(WifiListInfo.TYPE_AD);
            wifiListInfo.setAdInfo(mAdInfo);
            if (mData.size() > 3) {
                mData.add(3, wifiListInfo);
            }
            notifyDataSetChanged();
        }
    }

    public void setAdInfo(AdInfo adInfo) {
        if (adInfo != null && adInfo != this.mAdInfo) {
            Iterator<WifiListInfo> iterator = mData.iterator();
            while (iterator.hasNext()) {
                WifiListInfo next = iterator.next();
                if (next.getType() == WifiListInfo.TYPE_AD) {
                    iterator.remove();
                }
            }
            WifiListInfo wifiListInfo = new WifiListInfo();
            wifiListInfo.setType(WifiListInfo.TYPE_AD);
            wifiListInfo.setAdInfo(adInfo);
            if (mData.size() > 3) {
                mData.add(3, wifiListInfo);
            }
            mAdInfo = adInfo;
            notifyDataSetChanged();
        }
    }

    public void closeAdInfo() {
        Iterator<WifiListInfo> iterator = mData.iterator();
        while (iterator.hasNext()) {
            WifiListInfo next = iterator.next();
            if (next.getType() == WifiListInfo.TYPE_AD) {
                iterator.remove();
            }
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == WifiListInfo.TYPE_WIFI_INFO) {
            return new WifiListViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_wifi, parent, false));
        }
        if (viewType == WifiListInfo.TYPE_AD) {
            return new AdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_wifi_list_ad, parent, false));
        }
        return null;
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        final WifiListInfo wifiListInfo = mData.get(position);
        int type = wifiListInfo.getType();
        if (type == WifiListInfo.TYPE_WIFI_INFO) {
            WifiInfo wifiInfo = wifiListInfo.getWifiInfo();
            WifiListViewHolder holder = (WifiListViewHolder) viewHolder;
            holder.tvWifiSsid.setText(wifiInfo.getSsid());
            holder.tvWifiSsid.setSelected(wifiInfo.isConnected());
            holder.tvWifiInfo.setText(wifiInfo.getWifiInfo());
            holder.tvIsAvailable.setText(wifiInfo.getAvailableInfo());
            if (wifiInfo.getWifiCipherType() == WifiSupport.WifiCipherType.WIFICIPHER_NOPASS) {
                int level = wifiInfo.getLevel();
                if (level == 4) {
                    holder.ivWifiIntensity.setImageResource(R.drawable.icon_wifi01);
                }
                if (level == 3) {
                    holder.ivWifiIntensity.setImageResource(R.drawable.icon_wifi02);
                }
                if (level == 2) {
                    holder.ivWifiIntensity.setImageResource(R.drawable.icon_wifi03);
                }
                if (level == 1) {
                    holder.ivWifiIntensity.setImageResource(R.drawable.icon_wifi04);
                }
                if (level == 0) {
                    holder.ivWifiIntensity.setImageResource(R.drawable.icon_wifi05);
                }
            } else {
                int level = wifiInfo.getLevel();
                if (level == 4) {
                    holder.ivWifiIntensity.setImageResource(R.drawable.icon_wifi06);
                }
                if (level == 3) {
                    holder.ivWifiIntensity.setImageResource(R.drawable.icon_wifi07);
                }
                if (level == 2) {
                    holder.ivWifiIntensity.setImageResource(R.drawable.icon_wifi08);
                }
                if (level == 1) {
                    holder.ivWifiIntensity.setImageResource(R.drawable.icon_wifi09);
                }
                if (level == 0) {
                    holder.ivWifiIntensity.setImageResource(R.drawable.icon_wifi10);
                }
            }
            if (wifiInfo.isConnected()) {
                holder.tvWifiConnected.setVisibility(View.VISIBLE);
                holder.viewKeyTip.setVisibility(View.INVISIBLE);
            } else {
                holder.tvWifiConnected.setVisibility(View.INVISIBLE);
                if (wifiInfo.isConfig()) {
                    holder.viewKeyTip.setVisibility(View.VISIBLE);
                } else {
                    holder.viewKeyTip.setVisibility(View.INVISIBLE);
                }
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onWifiItemClick(wifiListInfo);
                    }
                }
            });
            holder.ivMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onWifiMore(wifiListInfo);
                    }
                }
            });
        }
        if (type == WifiListInfo.TYPE_AD) {
            final AdInfo adInfo = wifiListInfo.getAdInfo();
            AdViewHolder holder = (AdViewHolder) viewHolder;
            ImageLoader.getInstance().displayImage(mContext, adInfo.getIcon(), holder.ivAdImg);
            holder.tvTitle.setText(adInfo.getName());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onAdClick(adInfo);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        WifiListInfo wifiListInfo = mData.get(position);
        return wifiListInfo.getType();
    }

    public static interface ItemClickListener {
        void onAdClick(AdInfo adInfo);

        void onWifiItemClick(WifiListInfo wifiBean);

        void onWifiMore(WifiListInfo wifiListInfo);
    }

    public static class WifiListViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_wifi_intensity)
        ImageView ivWifiIntensity;
        @BindView(R.id.tv_wifi_ssid)
        TextView tvWifiSsid;
        @BindView(R.id.tv_wifi_info)
        TextView tvWifiInfo;
        @BindView(R.id.tv_is_available)
        TextView tvIsAvailable;
        @BindView(R.id.tv_wifi_connected)
        TextView tvWifiConnected;
        @BindView(R.id.view_key_tip)
        View viewKeyTip;
        @BindView(R.id.iv_wifi_more)
        ImageView ivMore;

        public WifiListViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class AdViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_ad)
        ImageView ivAdImg;
        @BindView(R.id.tv_title)
        TextView tvTitle;

        public AdViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
